<?php
namespace lib;
class Redis {
    private $config = null;
    private $client = null;
    public function __construct() {
        $this->client = new \Swoole\Coroutine\Redis();

        $redisConfig = Config::$redis;

        $rs = $this->client->connect($redisConfig['host'], $redisConfig['port']);
        if($rs == false){
            return false;
        }
        
        $this->client->auth($redisConfig['password']);
        if($rs == false){
            return false;
        }
    }
    
    public function connect() {
        
        return true;
    }
    
    public function isConnect() {
        
    }
    
    public function set($key, $value) {
        if(is_array($value)){
            $value = json_encode($value);
        }
        return $this->client->set($key, $value);
    }
    
    public function lpush($key, $value) {
        if(is_array($value)){
            $value = json_encode($value);
        }
        $this->client->lpush($key, $value);
    }
    
    public function get($key) {
        $result = '';
        try {
            $result = $this->client->get($key);
        } catch (\Exception $e) {
        }
        
        return $result;
    }
    
    public function del($key) {
        $this->client->del($key);
    }
    
    public function close() {
        $this->client->close();
    }
}